<?php
/**
 * Created by PhpStorm.
 * User: WINNERWLY
 * Date: 2018/8/8
 * Time: 9:07
 */

namespace app\index\controller;


use app\index\model\Bill;
use think\Controller;
use app\index\model\Cooperator as CooperatorModel;
use think\Request;

class Cooperator extends Controller
{
    public function index()
    {
        if (Request::instance()->isGet()) {
            $map = [];
            if (input('name')) {
                $map['name'] = ['like', '%' . input("name") . '%'];
            }
            if (input("startDate")){
                $map["create_time"]=['>',strtotime(input("startDate"))];
                $map["create_time"]=['<',strtotime(input("endDate"))];
            }
            $res = CooperatorModel::scope('index')
                ->page(input('page'), input('rows'))
                ->order("create_time desc")
                ->where($map)
                ->select();
            $total = CooperatorModel::scope('index')->count();

            if ($res) {
                return ajaxReturn(200, "查询成功", $res, $total);
            } else {
                return ajaxReturn(200, "未查询到信息");
            }
        } else {
            return json(["msg" => "参数错误"], 400);
        }
    }

    public function create()
    {
        if (Request::instance()->isPost()) {
            $model = new CooperatorModel;
            $res = $model->save([
                "name" => input('name'),
                "remark" => input('remark'),
            ]);
            if ($res) {
                return ajaxReturn(200, "新增成功");
            } else {
                return ajaxReturn(201, "新增失败");
            }
        } else {
            return json(["msg" => "参数错误"], 400);
        }
    }

    public function delete()
    {
        if (Request::instance()->isDelete()) {
            $model = CooperatorModel::get(input('ids'));
            $res = $model->save(["is_delete" => 1]);
            if ($res) {
                return ajaxReturn(200, "删除成功");
            } else {
                return ajaxReturn(201, "删除失败");
            }
        } else {
            return json(["msg" => "参数错误"], 400);
        }
    }

    public function update()
    {
        if (Request::instance()->isPost()) {
            $model = CooperatorModel::get(input("id"));
            $res = $model->save([
                "name" => input("name"),
                "remark" => input("remark")
            ]);
            if ($res) {
                return ajaxReturn(200, "更新成功");
            } else {
                return ajaxReturn(201, "更新失败");
            }
        } else {
            return json(["msg" => "参数错误"], 400);
        }
    }

    public function recharge()
    {
        if (Request::instance()->isPost()) {
            $model = CooperatorModel::get(input('id'));
            $bill = new Bill;
            $data = [
                "cooperator_id" => input("id"),
                "type" => 1,
                "amount" => input("amount"),
                "status" => "success",
                "create_user_id" => input("createUserId"),
            ];
            if (input("remark")) {
                $data["remark"] = input("remark");
            }
            $bill->save($data);
            $res = $model->save([
                "balance" => $model->balance + input('amount'),
            ]);
            if ($res) {
                return ajaxReturn(200, "充值成功");
            } else {
                return ajaxReturn(201, "充值失败");
            }
        } else {
            return json(["msg" => "参数错误"], 400);
        }
    }

}
